﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SpotlessWeb.Helpers
{
    public static class SelectListHelpers
    {
        public static IEnumerable<SelectListItem> GetSelectListFromEnumUsingValues<TEnum>()
        {
            var type = Enum.GetUnderlyingType(typeof(TEnum));
            var values = Enum.GetValues(typeof(TEnum));;
            var items = new List<SelectListItem>();
            foreach(var val in values)
            {
                items.Add(new SelectListItem(){Text = val.ToDisplayString(), Value = Convert.ChangeType(val, type).ToString()});
            }

            return items.AsEnumerable();
        }

        public static SelectList GetSelectListFromEnum<TEnum>()
        {
            var values = Enum.GetValues(typeof(TEnum))
                .Cast<TEnum>()
                .Select(x => new { Id = x, Name = x.ToDisplayString() });

            var tmp = new SelectList(values, "Id", "Name");
            return tmp;
        }

        /*public static SelectList GetSelectListFromEnum<TEnum>(this HtmlHelper html)
        {
            var values = Enum.GetValues(typeof(TEnum))
                .Cast<TEnum>()
                .Select(x => new { Id = x, Name = x.ToDisplayString() });

            var tmp = new SelectList(values, "Id", "Name");
            return tmp;
        }*/

        public static string ToDisplayString<TEnum>(this TEnum enumObj)
        {
            return enumObj.ToString().Replace("_", " ");
        }
    }
}
